package com.zhixi.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.springframework.stereotype.Component;

/**
 * @ClassName SendSms
 * @Author zhangzhixi
 * @Description 发送短信
 * @Date 2022-7-1 9:19
 * @Version 1.0
 */
@Component
public class SendSmsUtils {




    /**
     * 发送短信
     *
     * @param smsSdkAppId   签名ID
     * @param templateId    模板id
     * @param phoneNumber   手机号
     * @param templateParam 模板参数
     * @param secretId 腾讯云用户名秘钥
     * @param secretKey 腾讯云用户密码
     * @return 发送结果
     */
    public static String sendSms(String smsSdkAppId, String templateId, String phoneNumber, String templateParam,String secretId,String secretKey) {
        // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
        // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        SendSmsRequest req = new SendSmsRequest();
        String[] phoneNumberSet1 = {phoneNumber};
        req.setPhoneNumberSet(phoneNumberSet1);

        req.setSmsSdkAppId(smsSdkAppId);
        req.setSignName("志喜的小破站");
        req.setTemplateId(templateId);

        String[] templateParamSet1 = {templateParam};
        req.setTemplateParamSet(templateParamSet1);

        // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
        SendSmsResponse resp = null;
        try {
            resp = client.SendSms(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        // 输出json格式的字符串回包
        assert resp != null;
        return SendSmsResponse.toJsonString(resp);
    }
}
